$(document).ready(function(){
    /***
     *  mainBanner  begin
     ***/
    var mainBanner = '';
    //获取mainBanner列表函数
    function getBannerList(){
        getAjax(`${global.apiUrl}/get_community_member_center_banner`,function (res) {
            // console.log(res);
            var html = '';
            mainBanner = res;
            for(var i in res){
                html += `
                 <div class="banner-item">
                    <img src="${res[i].banner_img}" alt="" >
                    <div class="eventBox themeColor">
                        <div class="reset" data-toggle="modal" data-target="#myModal" data-id="${res[i].id}" data-type="2">修改</div>
                        <div class="delete" data-id="${res[i].id}" >删除</div>
                    </div>
                 </div>
            `
            }
            html += `
             <div class="banner-add" data-toggle="modal" data-target="#myModal" data-type="1">
                <img src="../../img/community-service/banner_add.png" alt="">
            </div>
        `
            $('#banner-box').html(html);
        },{
            community_id: global.community_id
        })
    }
    //获取mainBanner
    getBannerList();
    //mainBanner修改删除按钮
    $('#banner-box').on('mouseenter','.banner-item',function(){
        $(this).find('.eventBox').css('display','block');
        $(this).find('.eventBox').css('opacity','1');
        // $('.eventBox').css('display','block');
        // $('.eventBox').css('opacity','1');
    })
    $('#banner-box').on('mouseleave','.banner-item',function(){
        $(this).find('.eventBox').css('opacity','.3');
        $(this).find('.eventBox').css('display','none');
    })
    //模态框显示触发事件（添加  修改）
    $('#myModal').on('show.bs.modal',function(event){
        var btnThis = $(event.relatedTarget);
        var modal = $(this);
        // modal.find('.preview-img')
        modal.find('#imgUrl').val('');
        modal.find('.preview-img').html(`<div>图片预览</div>`);
        var type = btnThis.data('type');
        // console.log(type);
        if(type == 1){
            modal.find('#myModalLabel').text('添加BANNER');
            modal.find('#uploadBannerImg').attr('data-type','1');
            modal.find('#bannerId').val('');
        }
        else if(type == 2){
            modal.find('#myModalLabel').text('修改BANNER');
            modal.find('#uploadBannerImg').attr('data-type','2');
            var id = btnThis.data('id');
            // console.log(id,mainBanner);
            modal.find('#bannerId').val(id);
            for(var i in mainBanner){
                if(id == mainBanner[i].id){
                    modal.find('.preview-img').html(`<img src="${mainBanner[i].banner_img}" >`)
                }
            }
        }
    })
    //删除
    $('#banner-box').on('click','.delete',function(){
        var id = $(this).data('id');
        var r=confirm("是否要删除该轮播图？")
        if (r==true)
        {
            getAjax(`${global.apiUrl}/admin_operation_community_member_center_banner`,function(res){
                // console.log(res);
                if(res == 1){
                    getBannerList();
                    alert('删除成功！')
                }
            },{
                type:3,
                id:id
            })
        }
    })
    //设置imgUrl值到隐藏的input
    function setImgUrl(res){
        // console.log(res);
        document.getElementById('imgUrl').value = res;
    }
    //上传图片获取图片地址
    imgUpload('fileInput','fileImage',setImgUrl);
    //上传banner图片
    $('#uploadBannerImg').on('click',function(){
        var $imgUrl = $('#imgUrl').val();
        var type = $('#uploadBannerImg').data('type');
        // console.log($imgUrl,$bannerId);
        // console.log(type);
        if($imgUrl == ''){
            alert('请先选择轮播图片，再点击确认！')
        }else{
            if(type == 1){
                data = {
                    banner_img: $imgUrl,
                    community_id: global.community_id,
                }
            }else{
                var $bannerId = $('#bannerId').val();
                data = {
                    id: $bannerId,
                    banner_img: $imgUrl,
                    community_id: global.community_id,
                }
            }
            data = JSON.stringify(data);
            getAjax(`${global.apiUrl}/admin_operation_community_member_center_banner`,function(res){
                // console.log(res)
                if(res == 1){
                    getBannerList();
                    alert('上传成功！');
                    $('#myModal').modal('hide');
                }
            },{
                type:type,
                pro: data
            })
        }
    })
    /***
     *  mainBanner  end
     ***/

    //导航切换
    $('.nav-item').on('click',function(){
        $('.nav-item').removeClass('active');
        $(this).addClass('active');
        var index = $(this).index();
        // console.log(index);
        $('.item-box').css('display','none');
        var itemBox = document.getElementsByClassName('item-box');
        itemBox[index].style.display = 'block';
    })


    /***
    *  物业公告 begin
    ***/

    /***  公告列表  ***/
    //获取公告列表
    var noticeListPageNo = 1,showNum=10;
    var noticeList = '';
    function getNoticeList(p,showNum){
        getAjax(`${global.apiUrl}/get_community_member_center_property_notice`,function(res){
            // console.log(res);
            if(res.list.length == 0){
                $("#page").paging({
                    pageNo:0,
                    totalPage: 0, //总页数
                    totalSize: 0,
                    callback: function(num) {
                        getNoticeList(num,showNum)
                    }
                });
                var html = `
                        <tr>
                            <td colspan="3" align="center">暂无物业公告</td>
                        </tr>`
                $('#noticeList').html(html);
            }else{
                $("#page").paging({
                    pageNo:p,
                    totalPage: Math.ceil(res.count/showNum), //总页数
                    totalSize: res.count,
                    callback: function(num) {
                        getnoticeList(num,showNum)
                        noticeListPageNo = num
                    }
                });
                var html = '';
                noticeList = res.list;
                var list = res.list;
                for(var i in list){
                    html += `
                        <tr>
                            <td>${list[i].title}</td>
                            <td>${list[i].date}</td>
                            <td class="control-td themeColor">
                                <span class="reset" data-toggle="modal" data-target="#NoticeModal" data-id="${list[i].id}" data-type="2">修改</span>
                                <span class="delete" data-id="${list[i].id}" style="margin-left: 15px">删除</span>
                            </td>
                        </tr>
                `
                }
                $('#noticeList').html(html);
            }
        },{
            p:p,
            num:showNum,
            community_id: global.community_id,
            type: 2,
        })
    }
    getNoticeList(noticeListPageNo,showNum);

    /** 添加公告 **/
    $('#NoticeModal').on('show.bs.modal',function(event){
        var modal = $(this);
        var btnThis = $(event.relatedTarget);
        var type = btnThis.data('type');
        if(type == '1' || type == '2'){
            modal.find('#upType').val(type);//新增/修改
            var title = modal.find('#title');
            title.val('');
            $('#summernote').summernote({
                height: 300,
                lang: 'zh-CN',
                onImageUpload: function(files, editor, $editable) {
                    sendFile(files[0],editor,$editable);
                }
            })
            $('#summernote').code('');
            //根据type判断是2修改还是1新增
            if(type == 1){
                modal.find('#noticeTxt').text('添加公告');
            }else if(type == 2){
                modal.find('#noticeTxt').text('修改公告');
                var noticeId = btnThis.data('id')
                modal.find('#noticeId').val(noticeId);
                var noticeInfo = '';
                for(var i in noticeList){
                    if(noticeList[i].id == noticeId){
                        noticeInfo = noticeList[i]
                        break;
                    }
                }
                // console.log(noticeInfo)
                title.val(noticeInfo.title)
                $('#summernote').code(noticeInfo.content)
            }
        }
        
    })

    //提交按钮
    $("#noticeBtn").on('click',function(){
        var title = $("#title").val()
        var content = $('#summernote').code()
        if(title == '' || content == ''){
            alert('请将信息填写完整！')
            return
        }
        var type = $("#upType").val();
        // console.log('type:',type);
        var data = {}
        if(type == 1){
            //新增
            data = {
                "title": title,
                "content": content,
                "date": setTime(new Date()),
                "community_id": global.community_id
            }
        }else if(type == 2){
            //修改
            var id = $("#noticeId").val();
            data = {
                "id": id,
                "title": title,
                "content": content,
                "date": setTime(new Date()),
                "community_id": global.community_id
            }
        }
        data = JSON.stringify(data);
        // console.log(data);
        var text = '';
        if(type == 2){
            text = '公告信息，修改成功！';
        }else if(type == 1){
            text = '新的公告，添加成功！';
        }
        postAjax(`${global.apiUrl}/admin_operation_community_member_center_property_notice`,function(res){
            if(res == 1){
                alert(text);
                $('#NoticeModal').modal('hide');
                getNoticeList(publicWelfarePageNo,showNum)                
            }
        },{
            type:type,
            pro: data
        })
        
    })
    //删除
    $('#noticeList').on('click','.delete',function(){
        var id = $(this).data('id');
        var r=confirm("是否要删除该公告？")
        if (r==true)
        {
            getAjax(`${global.apiUrl}/admin_operation_community_member_center_property_notice`,function(res){
                // console.log(res);
                if(res == 1){
                    getNoticeList(publicWelfarePageNo,showNum)
                    alert('删除成功！')
                }
            },{
                type:3,
                id:id
            })
        }
    })

    
    /***
     * 物业公告部分 end
     ***/






    /***
     * 社区公益 begin
     ***/

    /*** banner ***/
    var publicWelfareBannerList = ''
    //社区公益banner
    function getPublicWelfareBannerList(){
        getAjax(`${global.apiUrl}/get_community_member_center_gy_banner`,function(res){
            // console.log(res);
            var html = '';
            publicWelfareBannerList = res;
            for(var i in res){
                html += `
                 <div class="banner-item">
                    <img src="${res[i].banner_img}" alt="" >
                    <div class="eventBox themeColor">
                        <div class="reset" data-toggle="modal" data-target="#publicWelfareBannerModal" data-id="${res[i].id}" data-type="2">修改</div>
                        <div class="delete" data-id="${res[i].id}" >删除</div>
                    </div>
                 </div>
            `
            }
            html += `
             <div class="banner-add" data-toggle="modal" data-target="#publicWelfareBannerModal" data-type="1">
                <img src="../../img/community-service/banner_add.png" alt="">
            </div>
        `
            $('#banner-item-box').html(html);
        },{
            community_id: global.community_id
        })
    }
    getPublicWelfareBannerList();
    
    /*** 模块轮播图 ***/
    $('#banner-item-box').on('mouseenter','.banner-item',function(){
        $(this).find('.eventBox').css('display','block');
        $(this).find('.eventBox').css('opacity','1');
    })
    $('#banner-item-box').on('mouseleave','.banner-item',function(){
        $(this).find('.eventBox').css('opacity','.3');
        $(this).find('.eventBox').css('display','none');
    })
    //modal 触发同时
    $('#publicWelfareBannerModal').on('show.bs.modal',function(event){
        var btnThis = $(event.relatedTarget);
        var modal = $(this);
        // modal.find('.preview-img')
        modal.find('#publicWelfareImgUrl').val('');
        modal.find('.preview-img').html(`<div>图片预览</div>`);
        var type = btnThis.data('type');
        // console.log(type);
        if(type == 1){
            modal.find('#publicWelfareTxt').text('添加BANNER');
            modal.find('#uploadPublicWelfareBannerImgBtn').attr('data-type','1');
            modal.find('#publicWelfareBannerId').val('');
        }
        else if(type == 2){
            modal.find('#publicWelfareTxt').text('修改BANNER');
            modal.find('#uploadPublicWelfareBannerImgBtn').attr('data-type','2');
            var id = btnThis.data('id');
            // console.log(id,mainBanner);
            modal.find('#publicWelfareBannerId').val(id);
            // console.log(publicWelfareBannerList);
            for(var i in publicWelfareBannerList){
                if(id == publicWelfareBannerList[i].id){
                    modal.find('.preview-img').html(`<img src="${publicWelfareBannerList[i].banner_img}" >`)
                }
            }
        }
    });
    function setPublicWelfareImgUrl(res){
        // console.log(res)
        document.getElementById('publicWelfareImgUrl').value = res;
    }
    imgUpload('publicWelfareInput','publicWelfareImage',setPublicWelfareImgUrl);

    //上传，修改
    $('#uploadPublicWelfareBannerImgBtn').on('click',function(){
        var $imgUrl = $('#publicWelfareImgUrl').val();
        var type = $('#uploadPublicWelfareBannerImgBtn').data('type');
        // console.log($imgUrl,$bannerId);
        // console.log(type);
        if($imgUrl == ''){
            alert('请先选择轮播图片，再点击确认！')
        }else{
            if(type == 1){
                data = {
                    banner_img: $imgUrl,
                    community_id: global.community_id,
                }
            }else{
                var $bannerId = $('#publicWelfareBannerId').val();
                data = {
                    id: $bannerId,
                    banner_img: $imgUrl,
                    community_id: global.community_id,
                }
            }
            data = JSON.stringify(data);
            postAjax(`${global.apiUrl}/admin_operation_community_member_center_gy_banner`,function(res){
                if(res == 1){
                    getPublicWelfareBannerList();
                    alert('上传成功！');
                    $('#publicWelfareBannerModal').modal('hide');
                }
            },{
                type:type,
                pro: data
            })
        }
    })
    //删除
    $('#banner-item-box').on('click','.delete',function(){
        var id = $(this).data('id');
        var r = confirm("是否要删除该轮播图？")
        if (r==true)
        {
            getAjax(`${global.apiUrl}/admin_operation_community_member_center_gy_banner`,function(res){
                if(res == 1){
                    getPublicWelfareBannerList();
                    alert('删除成功！')
                }
            },{
                type:3,
                id:id
            })
        }

    })

    /*** 添加 修改公益 ***/
    function setPublicWelfareImgUrl2(res){
        document.getElementById('publicWelfare-img-url').value = res;
    }
    imgUpload('publicWelfare-img-select','publicWelfare-img-show',setPublicWelfareImgUrl2);
    $('#PublicWelfareModal').on('show.bs.modal',function(event){
        var modal = $(this);
        var btnThis = $(event.relatedTarget);
        var type = btnThis.data('type');
        modal.find('#publicWelfareUpType').val(type);
        modal.find('#publicWelfareId').val('');
        var title = modal.find('#publicWelfaretitle');
        title.val('');
        var publicWelfareImg = modal.find('#publicWelfare-img-url');
        publicWelfareImg.val('');
        var publicWelfareAddress = modal.find('#publicWelfareAddress');
        publicWelfareAddress.val('')
        var publicWelfareDesc = modal.find('#publicWelfareDesc');
        publicWelfareDesc.val('');
        var start = modal.find('#publicWelfareStart');
        $("#publicWelfareStart").val('');
        var end = modal.find('#publicWelfareEnd');
        $("#publicWelfareEnd").val('');
    
        //根据type判断是2修改还是1新增
        if(type == 1){
            modal.find('#publicWelfareTxt').text('添加公益活动');
        }
        else if(type == 2){
            modal.find('#publicWelfareTxt').text('修改公益活动');
            var publicWelfareId = btnThis.data('id')
            modal.find('#publicWelfareId').val(publicWelfareId);
            var publicWelfareInfo = '';
            for(var i in publicWelfareList){
                if(publicWelfareList[i].id == publicWelfareId){
                    publicWelfareInfo = publicWelfareList[i]
                    break;
                }
            }     
            title.val(publicWelfareInfo.title);
            publicWelfareImg.val(publicWelfareInfo.title_img);
            modal.find('#publicWelfare-img-show').html(`<img src="${publicWelfareInfo.title_img}"/>`);
            publicWelfareAddress.val(publicWelfareInfo.address)
            start.val(publicWelfareInfo.date);
            end.val(publicWelfareInfo.close_time);
            publicWelfareDesc.val(publicWelfareInfo.content);
        }
    })

    //获取地址
    $('#publicWelfareAddress').on('input propertychange',function(){
        var keyword = $(this).val();
        // console.log(keyword);
        var region = '天津';
        if(keyword != ''){
            getAjax(`${global.apiUrl}/Api/Url/ceshi`,res =>{
                // console.log(res);
                if(res.status == 0){
                    var html = '';
                    for(var i in res.data){
                        html += `
                        <div class="address-list-item themeColor">
                            <div class="address-title">${res.data[i].title}</div>
                            <div class="address-address">${res.data[i].address}</div>
                            <input type="hidden" class="lat" value="${res.data[i].location.lat}">
                            <input type="hidden" class="lng" value="${res.data[i].location.lng}">
                        </div>
                    `
                    }
                    $('#publicWelfareAddress-List').html(html);
                }
            },{
                keyword:keyword,
                key:'6VNBZ-2YSC3-UAK3W-YDOS4-QLWCT-OWBXM',
                region:region
            })
        }else{
            $('#publicWelfareAddress-List').html('');
        }

    });
    $('#publicWelfareAddress-List').on('click','.address-list-item',function(){
        var address = $(this).find('.address-address').text();
        var title = $(this).find('.address-title').text();
        var lat = $(this).find('.lat').val();
        var lng = $(this).find('.lng').val();
        $('.address-list').html('');
        $('#publicWelfareAddress').val(`${title} (${address})`)
        $('#publicWelfare-address-lat').val(lat)
        $('#publicWelfare-address-lng').val(lng)
    })

    //提交按钮
    $("#publicWelfareBtn").on('click',function(){
        var title = $("#publicWelfaretitle").val()
        var publicWelfareImg = $('#publicWelfare-img-url').val();
        var publicWelfareAddress = $('#publicWelfareAddress').val();
        var publicWelfareDesc = $('#publicWelfareDesc').val();
        var start = $('#publicWelfareStart').val();
        var end = $('#publicWelfareEnd').val();
        if(title == '' || publicWelfareImg == '' || publicWelfareAddress == '' || publicWelfareDesc == '' || start == '' || end == ''){
            alert('请将信息填写完整！')
            return
        }
        var type = $("#publicWelfareUpType").val();
        // console.log('type:',type);
        var data = {}
        if(type == 1){
            data = {
                "title": title,
                "title_img": publicWelfareImg,
                "address": publicWelfareAddress,
                "content": publicWelfareDesc,
                "date": start,
                "status": 0,
                "close_time": end,
                "old_time": Date.parse(new Date(end.replace(/-/g, '/')))/1000,
                "community_id": global.community_id
            }
        }else if(type == 2){
            var id = $("#publicWelfareId").val();
            data = {
                "id": id,
                "title": title,
                "title_img": publicWelfareImg,
                "address": publicWelfareAddress,
                "content": publicWelfareDesc,
                "date": start,
                "status": 0,
                "close_time": end,
                "old_time": Date.parse(new Date(end.replace(/-/g, '/')))/1000,
                "community_id": global.community_id
            }
        }
        data = JSON.stringify(data);
        var text = '';
        if(type == 2){
            text = '公益活动，修改成功！';
        }else if(type == 1){
            text = '新的公益，添加成功！';
        }
        postAjax(`${global.apiUrl}/admin_operation_community_member_center_gy`,function(res){
            if(res == 1){
                alert(text);
                $('#PublicWelfareModal').modal('hide');
                getPublicWelfareList(publicWelfarePageNo,showNum)                
            }
        },{
            type:type,
            pro: data
        })
        
    })

    //获取公益列表
    var publicWelfarePageNo = 1,showNum=10;
    var publicWelfareList = '';
    getPublicWelfareList(publicWelfarePageNo,showNum)
    function getPublicWelfareList(p,showNum){
        getAjax(`${global.apiUrl}/get_community_member_center_gy`,function(res){
            // console.log(res);
            if(res.list.length == 0){
                $("#page2").paging({
                    pageNo:0,
                    totalPage: 0, //总页数
                    totalSize: 0,
                    callback: function(num) {
                        getPublicWelfareList(num,showNum)
                    }
                });
                var html = `
                        <tr>
                            <td colspan="5" align="center">暂无公益活动</td>
                        </tr>`
                $('#publicWelfareList').html(html);
            }else{
                $("#page2").paging({
                    pageNo:p,
                    totalPage: Math.ceil(res.count/showNum), //总页数
                    totalSize: res.count,
                    callback: function(num) {
                        getPublicWelfareList(num,showNum)
                        publicWelfarePageNo = num
                    }
                });
                var html = '';
                publicWelfareList = res.list;
                var list = res.list;
                for(var i in list){
                    html += `
                        <tr>
                            <td>${list[i].title}</td>
                            <td>${list[i].address}</td>
                            <td>${list[i].date}</td>
                            <td>${list[i].close_time}</td>
                            <td class="control-td themeColor">
                                <span class="education-reset" data-id="${list[i].id}" data-type="2" data-target="#PublicWelfareModal" data-toggle="modal">修改</span> 
                                <span class="publicWelfare-delete" style="margin-left: 15px" data-id="${list[i].id}">删除</span>
                                <span class="publicWelfare-signUplist" style="margin-left: 15px" data-id="${list[i].id}">查看报名列表</span>
                            </td>
                        </tr>
                    `
                }
                $('#publicWelfareList').html(html);
            }
        },{
        p:p,
        num:showNum,
        community_id: global.community_id
        })
    }

    /** 删除 **/
    $('#publicWelfareList').on('click','.publicWelfare-delete',function(){
        var publicWelfareId = $(this).data('id');
        var f = confirm('是否删除该公益活动');
        if(f){
            getAjax(`${global.apiUrl}/admin_operation_community_member_center_gy`,function(res){
                // console.log(res);
                if(res == 1){
                    alert('公益活动删除成功！');
                    getPublicWelfareList(publicWelfarePageNo,showNum);
                }
            },{
                type: 3,
                id: publicWelfareId
            })
        }
    })

    //查看报名列表
    $('#publicWelfareList').on('click','.publicWelfare-signUplist',function(){
        var publicWelfareId = $(this).data('id');
        $('#publicWelfareSignUpTable').css('display','block')
        getPublicWelfareSignUpList(publicWelfareSignUpPageNo,showNum,publicWelfareId)
    })


    //获取公益报名列表
    var publicWelfareSignUpPageNo = 1,showNum=10;
    var publicWelfareSignUpList = '';
    function getPublicWelfareSignUpList(p,showNum,classId){
        getAjax(`${global.apiUrl}/admin_get_community_member_center_up_gy`,function(res){
        //    console.log(res);
           if(res.list.length == 0){
               $("#page3").paging({
                   pageNo:0,
                   totalPage: 0, //总页数
                   totalSize: 0,
                   callback: function(num) {
                        getPublicWelfareSignUpList(num,showNum,classId)
                   }
               });
               var html = `
                       <tr>
                           <td colspan="6" align="center">暂无公益报名</td>
                       </tr>`
               $('#publicWelfareSignUpList').html(html);
           }else{
               $("#page3").paging({
                   pageNo:p,
                   totalPage: Math.ceil(res.count/showNum), //总页数
                   totalSize: res.count,
                   callback: function(num) {
                        getPublicWelfareSignUpList(num,showNum,classId)
                        publicWelfareSignUpPageNo = num
                   }
               });
               var html = '';
               publicWelfareSignUpList = res.list;
               var list = res.list;
               for(var i in list){
                    html += `
                        <tr>
                            <td>${list[i].hd_content.title}</td>
                            <td>${list[i].name}</td>
                            <td>${list[i].phone}</td>
                        </tr>
                    `
               }
               $('#publicWelfareSignUpList').html(html);
           }
       },{
        p:p,
        num:showNum,
        class_id: classId,
        community_id: global.community_id
       })
    }

    /***
     * 社区公益 end
     ***/


    /***
     * 社区活动 begin
     ***/

    /*** 添加 修改活动 ***/
    function setActivityImgUrl(res){
        document.getElementById('activity-img-url').value = res;
    }
    imgUpload('activity-img-select','activity-img-show',setActivityImgUrl);
    $('#ActivityModal').on('show.bs.modal',function(event){
        var modal = $(this);
        var btnThis = $(event.relatedTarget);
        var type = btnThis.data('type');
        modal.find('#activityUpType').val(type);
        modal.find('#activityeId').val('');
        var title = modal.find('#activitytitle');
        title.val('');
        var activityImg = modal.find('#activity-img-url');
        activityImg.val('');
        var activityAddress = modal.find('#activityAddress');
        activityAddress.val('')
        var activityDesc = modal.find('#activityDesc');
        activityDesc.val('');
        var activityPrice = modal.find('#activityPrice');
        activityPrice.val('');
        var start = modal.find('#activityStart');
        $("#activityStart").val('');
        var end = modal.find('#activityEnd');
        $("#activityEnd").val('');
    
        //根据type判断是2修改还是1新增
        if(type == 1){
            modal.find('#activityTxt').text('添加活动');
        }
        else if(type == 2){
            modal.find('#activityTxt').text('修改活动');
            var activityId = btnThis.data('id')
            modal.find('#activityId').val(activityId);
            var activityInfo = '';
            for(var i in activityList){
                if(activityList[i].id == activityId){
                    activityInfo = activityList[i]
                    break;
                }
            }     
            title.val(activityInfo.title);
            activityImg.val(activityInfo.title_img);
            modal.find('#activity-img-show').html(`<img src="${activityInfo.title_img}"/>`);
            activityAddress.val(activityInfo.address)
            start.val(activityInfo.date);
            end.val(activityInfo.close_time);
            activityDesc.val(activityInfo.content);
            activityPrice.val(activityInfo.price);
        }
    })

    //获取地址
    $('#activityAddress').on('input propertychange',function(){
        var keyword = $(this).val();
        // console.log(keyword);
        var region = '天津';
        if(keyword != ''){
            getAjax(`${global.apiUrl}/Api/Url/ceshi`,res =>{
                // console.log(res);
                if(res.status == 0){
                    var html = '';
                    for(var i in res.data){
                        html += `
                        <div class="address-list-item themeColor">
                            <div class="address-title">${res.data[i].title}</div>
                            <div class="address-address">${res.data[i].address}</div>
                            <input type="hidden" class="lat" value="${res.data[i].location.lat}">
                            <input type="hidden" class="lng" value="${res.data[i].location.lng}">
                        </div>
                    `
                    }
                    $('#activityAddress-List').html(html);
                }
            },{
                keyword:keyword,
                key:'6VNBZ-2YSC3-UAK3W-YDOS4-QLWCT-OWBXM',
                region:region
            })
        }else{
            $('#activityAddress-List').html('');
        }

    });
    $('#activityAddress-List').on('click','.address-list-item',function(){
        var address = $(this).find('.address-address').text();
        var title = $(this).find('.address-title').text();
        var lat = $(this).find('.lat').val();
        var lng = $(this).find('.lng').val();
        $('.address-list').html('');
        $('#activityAddress').val(`${title} (${address})`)
        $('#activity-address-lat').val(lat)
        $('#activity-address-lng').val(lng)
    })

    //提交按钮
    $("#activityBtn").on('click',function(){
        var title = $("#activitytitle").val()
        var activityImg = $('#activity-img-url').val();
        var activityAddress = $('#activityAddress').val();
        var activityDesc = $('#activityDesc').val();
        var start = $('#activityStart').val();
        var end = $('#activityEnd').val();
        var price = $('#activityPrice').val();
        if(title == '' || activityImg == '' || activityAddress == '' || activityDesc == '' || start == '' || end == '' || price==''){
            alert('请将信息填写完整！')
            return
        }
        var type = $("#activityUpType").val();
        // console.log('type:',type);
        var data = {}
        if(type == 1){
            data = {
                "title": title,
                "title_img": activityImg,
                "address": activityAddress,
                "content": activityDesc,
                "date": start,
                "status": 0,
                "close_time": end,
                "old_time": Date.parse(new Date(end.replace(/-/g, '/')))/1000,
                "price": price,
                "community_id": global.community_id
            }
        }else if(type == 2){
            var id = $("#activityId").val();
            data = {
                "id": id,
                "title": title,
                "title_img": activityImg,
                "address": activityAddress,
                "content": activityDesc,
                "date": start,
                "status": 0,
                "close_time": end,
                "old_time": Date.parse(new Date(end.replace(/-/g, '/')))/1000,
                "price": price,
                "community_id": global.community_id
            }
        }
        data = JSON.stringify(data);
        var text = '';
        if(type == 2){
            text = '社区活动，修改成功！';
        }else if(type == 1){
            text = '新的活动，添加成功！';
        }
        postAjax(`${global.apiUrl}/admin_operation_community_member_center_activities`,function(res){
            if(res == 1){
                alert(text);
                $('#ActivityModal').modal('hide');
                getActivityList(activityPageNo,showNum)                
            }
        },{
            type:type,
            pro: data
        })
        
    })

    //获取活动列表
    var activityPageNo = 1,showNum=10;
    var activityList = '';
    getActivityList(activityPageNo,showNum)
    function getActivityList(p,showNum){
        getAjax(`${global.apiUrl}/get_community_member_center_activities`,function(res){
            // console.log(res);
            if(res.list.length == 0){
                $("#page4").paging({
                    pageNo:0,
                    totalPage: 0, //总页数
                    totalSize: 0,
                    callback: function(num) {
                        getActivityList(num,showNum)
                    }
                });
                var html = `
                        <tr>
                            <td colspan="6" align="center">暂无社区活动</td>
                        </tr>`
                $('#activityList').html(html);
            }else{
                $("#page4").paging({
                    pageNo:p,
                    totalPage: Math.ceil(res.count/showNum), //总页数
                    totalSize: res.count,
                    callback: function(num) {
                        getActivityList(num,showNum)
                        activityPageNo = num
                    }
                });
                var html = '';
                activityList = res.list;
                var list = res.list;
                for(var i in list){
                    html += `
                        <tr>
                            <td>${list[i].title}</td>
                            <td>${list[i].address}</td>
                            <td>${list[i].date}</td>
                            <td>${list[i].close_time}</td>
                            <td>${list[i].price}</td>
                            <td class="control-td themeColor">
                                <span class="activity-reset" data-id="${list[i].id}" data-type="2" data-target="#ActivityModal" data-toggle="modal">修改</span> 
                                <span class="activity-delete" style="margin-left: 15px" data-id="${list[i].id}">删除</span>
                                <span class="activity-signUplist" style="margin-left: 15px" data-id="${list[i].id}">查看报名列表</span>
                            </td>
                        </tr>
                    `
                }
                $('#activityList').html(html);
            }
        },{
        p:p,
        num:showNum,
        community_id: global.community_id
        })
    }

    /** 删除 **/
    $('#activityList').on('click','.activity-delete',function(){
        var activityId = $(this).data('id');
        var f = confirm('是否删除该社区活动');
        if(f){
            getAjax(`${global.apiUrl}/admin_operation_community_member_center_activities`,function(res){
                // console.log(res);
                if(res == 1){
                    alert('社区活动删除成功！');
                    getActivityList(activityPageNo,showNum);
                }
            },{
                type: 3,
                id: activityId
            })
        }
    })

    //查看报名列表
    $('#activityList').on('click','.activity-signUplist',function(){
        var activityId = $(this).data('id');
        $('#activitySignUpTable').css('display','block')
        getActivitySignUpList(activitySignUpPageNo,showNum,activityId)
    })


    //获取活动报名列表
    var activitySignUpPageNo = 1,showNum=10;
    var activitySignUpList = '';
    function getActivitySignUpList(p,showNum,classId){
       getAjax(`${global.apiUrl}/admin_get_community_member_center_up_hd`,function(res){
            //  console.log(res);
           if(res.list.length == 0){
               $("#page5").paging({
                   pageNo:0,
                   totalPage: 0, //总页数
                   totalSize: 0,
                   callback: function(num) {
                        getActivitySignUpList(num,showNum,classId)
                   }
               });
               var html = `
                       <tr>
                           <td colspan="3" align="center">暂无公益报名</td>
                       </tr>`
               $('#activitySignUpList').html(html);
           }else{
               $("#page5").paging({
                   pageNo:p,
                   totalPage: Math.ceil(res.count/showNum), //总页数
                   totalSize: res.count,
                   callback: function(num) {
                        getActivitySignUpList(num,showNum,classId)
                        activitySignUpPageNo = num
                   }
               });
               var html = '';
               activitySignUpList = res.list;
               var list = res.list;
               for(var i in list){
                   html += `
                       <tr>
                           <td>${list[i].hd_content.title}</td>
                           <td>${list[i].name}</td>
                           <td>${list[i].phone}</td>
                       </tr>
                   `
               }
               $('#activitySignUpList').html(html);
           }
       },{
        p:p,
        num:showNum,
        class_id: classId,
        community_id: global.community_id
       })
    }

    /***
     * 社区活动 end
     ***/


    /***
     * 社区活动展示 begin
     ***/

    var imgurl;
    var imgs = [];  //商品head展示图片
    var imgArr = imgUploadShow('content-img-select','content-img-show');
    //新图删除
    $('#content-img-show').on('click','.cancle',function(){
        var index = $(this).parent().index();
        imgArr.splice(index,1);
        var contentImgList = document.getElementById('content-img-show');
        // // console.log(contentImgList);
        var contentNewImgList = contentImgList.getElementsByClassName('new')[0];
        var contentImgItem = document.getElementsByClassName('content-img-item');
        // // console.log(contentImgItem);
        contentNewImgList.removeChild(contentImgItem[index])

    })
    //旧图删除
    $('#content-img-show').on('click','.del',function(){
        var index = $(this).parent().index();
        // console.log(index);
        imgs.splice(index,1);
        // console.log(imgs)
        var contentImgList = document.getElementById('content-img-show');
        // // console.log(contentImgList);
        var contentNewImgList = contentImgList.getElementsByClassName('old')[0];
        var contentImgItem = document.getElementsByClassName('content-old-img-item');
        // // console.log(contentImgItem);
        contentNewImgList.removeChild(contentImgItem[index])
        // // console.log(imgs);
    })
    $('#upload-content-img').on('click',function(){
        allImgUpload(imgArr,setContentImgUrl)
    });
    function setContentImgUrl(res){
        console.log(res)
        if(res.length != 0){
            imgurl = {
                url : res
            } ;
            imgs.push(imgurl)
            // console.log(imgs);
            $('#upload-content-img').css('display','none');
        }
    }
    //添加新管理人员
    $('#ManagementModal').on('show.bs.modal',function(event){
        var modal = $(this);
        var btnThis = $(event.relatedTarget);
        var type = btnThis.data('type');
        modal.find('#activityUpType2').val(type);
        var activityContent = modal.find('#activityContent');
        activityContent.val('');
        modal.find('#content-img-show .old').html('');
        modal.find('#content-img-show .new').html('');//可能存在bug
        $('#upload-content-img').css('display','none');
        if(type == 1){
            //新增
            modal.find('#managementLabel').text('添加活动展示');
        }else{
            modal.find('#GoodsModalLabel').text('修改活动展示');
            var activityId = btnThis.data('id')
            modal.find('#activityId2').val(activityId);
            var activityInfo = '';
            for(var i in activityNewList){
                if(activityNewList[i].id == activityId){
                    activityInfo = activityNewList[i]
                    break;
                }
            }
            activityContent.val(activityInfo.content)
            imgs = activityInfo.img;
            var imgs = activityInfo.img;
            // // console.log(head_banner);
            var html = '';
            for(var j in imgs){
                html+= `
                    <div class="content-old-img-item">
                        <img src="${imgs[j].url}" alt="">
                        <div class="del themeColor">删 除</div>
                    </div>
                `
            }
            modal.find('#content-img-show .old').html(html);
        }
    })

    /** 添加 修改 **/
    $('#addManagement').on('click',function(){
        //获取名称
        var activityType = $("#activityUpType2").val();
        var activityContent = $('#activityContent').val();
        //数据
        if(activityContent == ''){
            alert('请将信息填写完整！')
            return
        }
        var data = {}
        if(activityType == 1){
            //新增
            data={
                "content": activityContent,
                "img": imgs,
                "date": setTime(new Date()),
                "community_id": global.community_id,
                "user_id": global.shopUserId
            }
        }else{
            //修改
            var id = $("#activityId2").val();
            data = {
                "id": id,
                "content": activityContent,
                "img": imgs,
                "date": setTime(new Date()),
                "community_id": global.community_id,
                "user_id": global.shopUserId
              }
        }

        data = JSON.stringify(data);
        console.log(data)
        var text = ''
        if(activityType == 2){
            text = '活动展示，修改成功！';
        }else if(activityType == 1){
            text = '新的活动展示，添加成功！';
        }
        console.log(activityType)
        postAjax(`${global.apiUrl}/operation_community_member_center_hd_news`,function(res){
            console.log(res)
            if(res == 1){
                alert(text);
                $('#ManagementModal').modal('hide');
                getActivityNewList(activityNewPageNo,showNum);         
            }
        },{
            type: activityType,
            pro: data
        })
    })

    //获取活动列表
    var activityNewPageNo = 1,showNum=10;
    var activityNewList = ''
    getActivityNewList(activityNewPageNo,showNum)
    function getActivityNewList(p,showNum){
        getAjax(`${global.apiUrl}/get_community_member_center_hd_news`,function(res){
            console.log(res);
            if(res.list.length == 0){
                $("#page6").paging({
                    pageNo:0,
                    totalPage: 0, //总页数
                    totalSize: 0,
                    callback: function(num) {
                        getActivityNewList(num,showNum)
                    }
                });
                var html = `
                        <tr>
                            <td colspan="6" align="center">暂无社区活动展示</td>
                        </tr>`
                $('#activityNewList').html(html);
            }else{
                $("#page6").paging({
                    pageNo:p,
                    totalPage: Math.ceil(res.count/showNum), //总页数
                    totalSize: res.count,
                    callback: function(num) {
                        getActivityNewList(num,showNum)
                        activityNewPageNo = num
                    }
                });
                var html = '';
                activityNewList = res.list
                var list = res.list;
                for(var i in list){
                    html += `
                        <tr>
                            <td class="headImg">
                                <img src="${list[i].user_content.headimgurl}"></img>
                            </td>
                            <td>${list[i].user_content.nickname}</td>
                            <td class="control-td themeColor">
                                <span class="activityNew-check" data-content="${list[i].content}" data-target="#activityNewModal" data-toggle="modal">查看内容</span> 
                            </td>
                            <td>
                                ${getImg(list[i].img)}
                            </td>
                            <td>${list[i].date}</td>
                            <td class="control-td themeColor">
                                <span class="reset" data-toggle="modal" data-target="#ManagementModal" data-id="${list[i].id}" data-type="2">修改</span>
                                <span class="delete" data-id="${list[i].id}" style="margin-left: 15px">删除</span>
                            </td>
                        </tr>
                    `
                }
                $('#activityNewList').html(html);
            }
        },{
            p:p,
            num:showNum,
            community_id: global.community_id
        })
    }

    function getImg(list){
        // console.log(list)
        var html = ''
        if(list){
            if(list.length != 0){
                for(var i in list){
                    html += `<span><img style="width: 40px;height: 40px;margin-right: 10px" src="${list[i].url}"></img></span>`
                }
            }
        }
        return html
    }

    /** 删除 **/
    $('#activityNewList').on('click','.delete',function(){
        var activityId = $(this).data('id');
        var f = confirm('是否删除该活动展示');
        if(f){
            getAjax(`${global.apiUrl}/operation_community_member_center_hd_news`,function(res){
                console.log(res);
                if(res == 1){
                    alert('活动展示删除成功！');
                    getActivityNewList(activityNewPageNo,showNum);
                }
            },{
                type:3,
                id: activityId
            })
        }

    })

    $('#activityNewModal').on('show.bs.modal',function(event){
        var modal = $(this);
        var btnThis = $(event.relatedTarget);
        var content = btnThis.data('content');
        var activityNewTxtAre = modal.find('#activityNewTxtAre').text('');
        activityNewTxtAre.text(content)
    })

    /***
     * 社区活动展示 end
     ***/
});
